//
// Created by 抑~风 on 2022/12/31.
//

#include "timeUtil.h"

// time转格式化字符串=====================================
namespace CWJ::TIME {
    std::string ShowDateTime(const tm &t, const string &format) {
        char s[100];
        strftime(s, sizeof(s), format.c_str(), &t);
        return string(s);
    }


    std::string ShowDateTime(const time_t &t, const string &format) {
        tm _tm;

        _tm = *gmtime(&t);

        return ShowDateTime(_tm, format);
    }

    std::string ShowDateTime(const tm &t, const char &dateDiv = '-', const char &timeDiv = ':') {
        ostringstream format;
        format << "%Y" << dateDiv << "%m" << dateDiv << "%d" << ' ';
        format << "%H" << timeDiv << "%M" << timeDiv << "%S";

        return ShowDateTime(t, format.str());
    }

    std::string ShowDateTime(const time_t &t, const char &dateDiv = '-', const char &timeDiv = ':') {
        ostringstream format;
        format << "%Y" << dateDiv << "%m" << dateDiv << "%d" << ' ';
        format << "%H" << timeDiv << "%M" << timeDiv << "%S";

        return ShowDateTime(t, format.str());
    }

    std::string ShowYMD(const time_t &t, const char &dateDiv = '-') {
        ostringstream format;
        format << "%Y" << dateDiv << "%m" << dateDiv << "%d";
        cout << format.str() << endl;
        return ShowDateTime(t, format.str());
    }

    std::string ShowHMS(const time_t &t, const char &timeDiv = ':') {
        ostringstream format;
        format << "%H" << timeDiv << "%M" << timeDiv << "%S";

        return ShowDateTime(t, format.str());
    }

    std::string ShowHM(const time_t &t, const char &timeDiv = ':') {
        ostringstream format;
        format << "%H" << timeDiv << "%M";
        return ShowDateTime(t, format.str());
    }
}